<?php


namespace IDock\Insurance\MACN;

use IDock\IDock\IDockInterface;
use IDock\Insurance\MACN\Request\ClauseListRequest;
use IDock\Insurance\MACN\Request\ClearCacheRequest;
use IDock\Insurance\MACN\Request\EPolicyDownloadRequest;
use IDock\Insurance\MACN\Request\PolicyInfoRequest;
use IDock\Insurance\MACN\Request\PremiumInquiryRequest;
use IDock\Insurance\MACN\Request\ProposalRecallRequest;
use IDock\Insurance\MACN\Request\ProposalStatusRequest;
use IDock\Insurance\MACN\Request\RenewalInquiryRequest;
use IDock\Insurance\MACN\Request\UnderwritingRequest;
use IDock\Insurance\MACN\Request\UploadFileRequest;
use IDock\Insurance\MACN\Request\UploadResultQueryRequest;
use IDock\Insurance\MACN\Request\ValidateCarRequest;
use IDock\Insurance\MACN\Request\VehicleModelRequest;
use IDock\Insurance\MACN\Response\ClauseListResponse;
use IDock\Insurance\MACN\Response\ClearCacheResponse;
use IDock\Insurance\MACN\Response\EPolicyDownloadResponse;
use IDock\Insurance\MACN\Response\InsurancePayUrlResponse;
use IDock\Insurance\MACN\Response\NonCarProductListResponse;
use IDock\Insurance\MACN\Response\PolicyInfoResponse;
use IDock\Insurance\MACN\Response\PremiumInquiryResponse;
use IDock\Insurance\MACN\Response\ProposalRecallResponse;
use IDock\Insurance\MACN\Response\ProposalStatusResponse;
use IDock\Insurance\MACN\Response\RenewalInquiryResponse;
use IDock\Insurance\MACN\Response\UnderwritingResponse;
use IDock\Insurance\MACN\Response\UploadFileResponse;
use IDock\Insurance\MACN\Response\UploadResultQueryResponse;
use IDock\Insurance\MACN\Response\ValidateCarResponse;
use IDock\Insurance\MACN\Response\VehicleActualValueResponse;
use IDock\Insurance\MACN\Response\VehicleModelResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\PolicyPaymentPrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

class MACNConstruct extends Builder implements IDockInterface
{
    protected $config = [];

    public function __construct($config)
    {
        $this->config = $config;

        $this->userId = $config['userId'] ?? '';
        $this->passWord = $config['passWord'] ?? '';
        $this->interSalesmanCode = $config['interSalesmanCode'] ?? '';
        $this->queryType = $config['queryType'] ?? '';
        $this->queryDetail = $config['queryDetail'] ?? '';
        $this->flag = $config['flag'] ?? '';
        $this->ip = $config['ip'] ?? '';
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return RenewalInquiryResponse
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor): RenewalInquiryResponse
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);

        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return VehicleActualValueResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): VehicleActualValueResponse
    {
        $response = new VehicleActualValueResponse();
        $response->setCode(200);
        $response->setVisitor($vehicleActualValueVisitor);
        $response->printerInit();
        //$vehicleActualValuePrinter = new VehicleActualValuePrinter();
        //$vehicleActualValuePrinter->actualValue = 0;
        //$response->printer = $vehicleActualValuePrinter;
        return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return ClauseListResponse|PremiumInquiryResponse
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        $request = new PremiumInquiryRequest();
        $request->setVisitor($premiumInquiryVisitor);
//        global $testResponse;
//        $testResponse = '';

        /*** @var PremiumInquiryResponse $response */
        $response = $this->execute($request);
        $printer = $response->printer;

        if (!$response->isSuccess()) {
            return $response;
        }

        if (!empty($response->printer->argc['askCalcNo'])) {
            $premiumInquiryVisitor->getExtInfoVisitor()->argc['askCalcNo'] = $response->printer->argc['askCalcNo'] ?? '';
            $responseA = $this->getClauseList($premiumInquiryVisitor, $response->printer);

            //跳过特约报错-后续拓展
            //if (!$responseA->isSuccess()) {
            //    return $responseA;
            //}
        }
        $response->transfer($premiumInquiryVisitor->isNewEnergy);

        //非车回显
        if (!empty($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList())) {
            $nonCarInquiryPrinter = new NonCarInquiryPrinter();
            $totalPremium = 0;
            foreach ($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList() as $item) {
                /*** @var $item NonCarProductVisitor */
                $single = new NonCarInquirySinglePrinter();
                $single->premium = $item->premium;
                $single->planCode = $item->planCode;
                $single->productCode = $item->productCode;
                $single->statusCode = 'success';
                $totalPremium += $single->premium * $item->insuredNum;
                $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
            }
            $nonCarInquiryPrinter->totalPremium = $totalPremium;
            $printer->nonCarInquiryPrinter = $nonCarInquiryPrinter;
        }
        $response->printer = $printer;
        return $response;
    }

    /**
     * 缓存清理
     * @param $vin
     * @param string $type
     * @return ClearCacheResponse
     */
    public function clearCache($vin, string $type = ''): ClearCacheResponse
    {
        $request = new ClearCacheRequest();
        $request->vin = $vin;
        $request->type = $type;

        /*** @var $response ClearCacheResponse */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return UnderwritingResponse
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): UnderwritingResponse
    {
        //文件单独上传
        $files = $premiumInquiryVisitor->getFileList();
        $uploadRequest = new UploadFileRequest();
        $uploadRequest->setVisitor($premiumInquiryVisitor);

        foreach ($files as $file) {
            $uploadRequest->file = $file;
            $this->execute($uploadRequest);
        }

        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var UnderwritingResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 支付
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return InsurancePayUrlResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): InsurancePayUrlResponse
    {
        $response = new InsurancePayUrlResponse();
        $response->setCode(200);
        $printer = new PolicyPaymentPrinter();
        $printer->payUrl = $paymentVisitor->argc['payUrl'] ?? '';
        $response->printer = $printer;
        return $response;
    }

    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return UploadFileResponse
     */
    public function uploadFile(UploadFileVisitor $uploadFileVisitor): UploadFileResponse
    {
        $response = new UploadFileResponse();
        $response->setCode(200);
        $response->printerInit();
        return $response;
    }

    /**
     * 投保单状态查询
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return ProposalStatusResponse
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): ProposalStatusResponse
    {
        $request = new ProposalStatusRequest();
        $request->setVisitor($proposalStatusVisitor);

        /*** @var ProposalStatusResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 撤单接口
     * @param ProposalRecallVisitor $revokeVisitor
     * @return ProposalRecallResponse
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): ProposalRecallResponse
    {
        $request = new ProposalRecallRequest();
        $request->setVisitor($revokeVisitor);

        /*** @var ProposalRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return EPolicyDownloadResponse
     */
    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): EPolicyDownloadResponse
    {
        $request = new EPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var EPolicyDownloadResponse $response */
        $response = $this->execute($request);
        $response->biPolicy = $EPolicyVisitor->biPolicyNo;
        $response->ciPolicy = $EPolicyVisitor->ciPolicyNo;
        $response->printerInit();
        if (isset($EPolicyVisitor->argc['acciPolicyNo'])) {
            $response = $response->setNoCar($EPolicyVisitor->argc['acciPolicyNo']);
        }
        return $response;
    }

    /**
     * 核保撤回
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return mixed|void
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): ProposalRecallResponse
    {
        $request = new ProposalRecallRequest();
        $request->setVisitor($recallVisitor);

        /*** @var ProposalRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 保单详情
     * @param PolicyInfoVisitor $policyInfoVisitor
     * @return PolicyInfoResponse
     */
    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): PolicyInfoResponse
    {
        $request = new PolicyInfoRequest();
        $request->setVisitor($policyInfoVisitor);

        /*** @var $response PolicyInfoResponse */
        $response = $this->execute($request);
        return $response;
    }

    public function getClauseList(PremiumInquiryVisitor $premiumInquiryVisitor, PremiumInquiryPrinter $printer): ClauseListResponse
    {
        sleep(5);
        $request = new ClauseListRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var ClauseListResponse $response */
        $response = $this->execute($request);
        $printer->specialClausesList = [];
        if ($response->isSuccess()) {

            /*** @var PremiumInquiryPrinter $clauseList */
            $clauseList = $response->printer;
            $printer->specialClausesList = $clauseList->specialClausesList;
        }
        return $response;
    }

    public function uploadResultQuery($appNo): UploadResultQueryResponse
    {
        $request = new UploadResultQueryRequest();
        $request->setAppNo($appNo);
        /**
         * @var UploadResultQueryResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return NonCarProductListResponse
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductListResponse
    {
        $response = new NonCarProductListResponse(200);
        $response->printerInit();
        return $response;
//        $request = new NonCarProductListRequest();
//        $request->setVisitor($nonCarProductListVisitor);
//        /**
//         * @var NonCarProductListResponse $response
//         */
//        $response = $this->execute($request);
//        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor)
    {
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter)
    {
    }

    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        /*** @var ValidateCarResponse $response */
        $response = $this->execute(new ValidateCarRequest());

        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
}
